$(function(){
	initSubmitEvent();
	initFormEvent();
});

function initSubmitEvent(){
	$("#submitForm").validate({
		debug: true, //调试模式取消submit的默认提交功能   
        focusInvalid: false, 
        onkeyup: false,   
        submitHandler: function(form){ 
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
            	jQuery.data(this, "disabledOnSubmit", {submited : true});
            	form.submit();
            } else {
            	// repeat submit ignore;
            }
        },
        success:function(element){
        	
        },
		errorPlacement : function(error, element) {
			if(error.text()){
				if(element.next("span[class='red']").size() == 0){
					element.after("<span class='red'>" + error.text() +"</span>");
				}else{
					element.next("span[class='red']").text(error.text());
				}
			}else{
				element.next("span[class='red']").remove();
			}
		},
        rules:{
        	fundAccount:{required:true},
        	goodsCode:{required:true},
        	entrustPrice:{required:true,min:0,isIntDigit2:true},
        	entrustAmount:{required:true,min:1,maxlength:10,digits:true}
        },
        messages:{
        	fundAccount:{required:"账户必填"},
        	goodsCode:{required:"商品必填"},
        	entrustPrice:{required:"请输入委售价格",isIntDigit2:"请输入正确的价格,小数最多2位"},
        	entrustAmount:{required:"请输入委售数量"}
        }	
	});
}

function initFormEvent(){
	$("input[name=fundAccount]").click(function(obj){
		openPupWindow("/account/getAccount.htm?callBack=selectUserCallBack");
	});
	$("input[name=goodsCode]").click(function(obj){
		openPupWindow("/goods/getGoods.htm?callBack=selectGoodsCallBack");
	});
}

function selectUserCallBack(obj){
	$("input[name=fundAccount]").val(obj.fundAccount);
	$("input[name=userId]").val(obj.userId);
	$("#userName").text(obj.userName);
}

function selectGoodsCallBack(obj){
	$("input[name=goodsId]").val(obj.goodsId);
	$("input[name=goodsCode]").val(obj.goodsCode);
	$("#goodsName").text(obj.goodsName);
/*
	$("input[name=entrustPrice]").val(obj.costPrice);
*/
}